package com.financing.service.invest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.financing.model.invest.Bid;
import com.financing.model.invest.Repay;
import com.financing.service.BaseService;
import com.financing.util.DateUtil;
import com.financing.util.Money;
import com.financing.util.Str;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class BidService extends BaseService<Bid>{
	
	/**
	 * 查找推荐标 
	 */
	public Bid recommand() { 
		return Bid.me.findFirst("select *, apply_unit+virtual_unit as avunit from bid where type='first' and (status='waiting' or status='repaying'  or status='full' or status='trading' or status='finish') order by create_time desc");
	}
	
	/**
	 * 设为完结标的
	 */
	public boolean setFinish(int bidId) { 
		List<Repay> repays = Repay.me.find("select * from repay where bid_id=? AND status<>'success'", bidId);
		if(repays.size()==0) {
			// 更新标的
			Bid b = Bid.me.findById(bidId);
			b.set("status", "finish").setActualRepayTime(new Date()).update();
			// 更新交易记录
			Db.update("update trade set status='finish' where status='repaying' and bid_id=?", b.getId());
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 分页信息
	 */
	public Page<Bid> list(int page, int size, Map<String, String> map, String orderBy) {
		if(Str.isBlank(orderBy)) {
			orderBy = "order by status desc, start_time desc";
		}
		return Bid.me.paginate(page, size, buildSql("select *, apply_unit+virtual_unit as avunit", "bid",Arrays.asList("rate","fund","day_limit","create_time","start_time","end_time","name","status","type"),map,orderBy));
	}

	/**
	 * 获取所有超时投资的标的
	 * @return 
	*/
	public List<Bid> expireInvest() {
		return Bid.me.find("select * from bid where status='trading' and end_time<=?", DateUtil.getNowDateAndTime());
	}
	
	/**
	 * 更新所有可以还款的全虚增标的为已还款
	*/
	public void finishVirtual() {
		Db.update("update bid set status='finish', actual_repay_time=? where status='repaying' and all_virtual='1' and invest_end_time<=?",DateUtil.getNowDateAndTime(),DateUtil.getNowDate()+" 23:59:59");
	}
	
	/**
	 * 获取所有募集中的标的
	*/
	public List<Bid> findByStatus(String status) {
		return Bid.me.find("select * from bid where status=? order by period asc", status);
	}

	/**
	 * 根据状态和期限获取对象
	 */
	public Bid findByStausAndTypeAndDay(String status, String type, String day) {
		return Bid.me.findFirst("select *, apply_unit+virtual_unit as vunit from bid where status=? AND type=? AND day_limit=? ORDER BY create_time DESC", status, type, day);
	}

	/**
	 * 判断同名标的
	 */
	public Bid findByName(String name) {
		return Bid.me.findFirst("select * from bid where name=?", name);
	}

	/**
	 * 获取流标过期标的
	 */
	public List<Bid> findFlowBid() {
		return Bid.me.find("select * from bid where status='trading' AND type<>'experi' AND end_time<=?", DateUtil.getNowDateAndTime());
	}

	/**
	 * 根据条件获取总金额
	 */
	public List<Bid> findByParams(Map<String, String> map) {
		return Bid.me.find(buildSql("bid",Arrays.asList("borrower", "name"),map));
	}

	/**
	 * 统计所有募集金额
	 */
	public double total() {
		Bid b = Bid.me.findFirst("select SUM(amount) as total from bid");
		return Money.get(b.getDouble("total"));
	}
	
	/**
	 * 刷新标的状态
	 */
	public void refresh() {
		Db.update("UPDATE bid SET status='trading' WHERE start_time<=? AND status='waiting' ", new Date());
	}
}
